package com.cta.view.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cta.constant.ProfileColumnMapping;
import com.cta.entity.Profile;
import com.cta.service.ProfileService;
import com.cta.view.json.BaseJson;
import com.cta.view.json.JsonObject;
import com.cta.view.json.ProfileJson;

@Controller
public class ProfileController extends BasicController {

	@Autowired
	private ProfileService profileService;

	@Autowired
	private MessageSource messageSource;
	
	private static ObjectMapper mapper = new ObjectMapper();

	/**
	 * list all profile of a pbx
	 * 
	 * @param locale
	 * @param trunkId
	 * @return
	 */
	@RequestMapping(value = "/list-profile", method = RequestMethod.GET)
	@ResponseBody
	public JsonObject<ProfileJson> listProfile(Locale locale, Long pbxId) {
		try {
			List<Profile> profiles = profileService.findProfileByPBXId(pbxId);
			List<ProfileJson> jsonProfile = new ArrayList<ProfileJson>(profiles.size());
			for (Profile p : profiles) {
				jsonProfile.add(new ProfileJson(String.valueOf(p.getId()), p.getColumn(), p.getName(), p.getDescription(), String
						.valueOf(p.getNumOfBytes()), p.getColumnMapping(), String.valueOf(p.getSpace())));
			}
			return JsonObject.newJsonObject(true, jsonProfile);
		} catch (Exception e) {
			log.error("Error: ", e);
			return JsonObject.emptyJsonObject();
		}
	}
	
	@RequestMapping(value = "/list-profile-column", method = RequestMethod.GET)
	@ResponseBody
	public JsonObject<BaseJson> listProfileColumn(Locale locale) {
		try {
			List<BaseJson> jsonProfile = new ArrayList<BaseJson>(70);
			for (ProfileColumnMapping p : ProfileColumnMapping.values()) {
				jsonProfile.add(new BaseJson(null, p.getColumn(), p.getName(), p.getDescriptionKey()));
			}
			Collections.sort(jsonProfile, new Comparator<BaseJson>(){

				@Override
				public int compare(BaseJson o1, BaseJson o2) {
					return o1.getName().compareTo(o2.getName());
				}
				
			});
			return JsonObject.newJsonObject(true, jsonProfile);
		} catch (Exception e) {
			log.error("Error: ", e);
			return JsonObject.emptyJsonObject();
		}
	}
	
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/save-profile", method = RequestMethod.POST)
	@ResponseBody
	public String createProfile(Locale locale, String profileData, String timeformat,
			String dateformat, String durationformat, Long pbxId) {
		try {
			List<Object> readValue = mapper.readValue(profileData, ArrayList.class);
			profileService.saveProfile(readValue, timeformat, dateformat, durationformat, pbxId);
			return String.format("{'success':true, msg:'%s'}", "Save profile ok");
		} catch (Exception e) {
			log.error("Error: ", e);
			return String.format("{'success':false, msg:'%s'}", "Save profile fail");
		}
	}
	
	@RequestMapping(value = "/delete-profile", method = RequestMethod.POST)
	@ResponseBody
	public String createProfile(Locale locale, Long profileId) {
		try {
			profileService.deleteProfile(profileId);
			return String.format("{'success':true, msg:'%s'}", "Delete profile ok");
		} catch (Exception e) {
			log.error("Error: ", e);
			return String.format("{'success':false, msg:'%s'}", "Delete profile fail");
		}
	}
}
